<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.name')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :placeholder="$t('common.form.rule.input',{label: $t('columns.name')})"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.shapeType')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shapeType">
              <a-select v-model="model.shapeType" :placeholder="$t('common.form.rule.input',{label: $t('columns.shapeType')})">
                <a-select-option value="circle">{{$t('dict.type.circle')}}</a-select-option>
                <a-select-option value="polygon">{{$t('dict.type.polygon')}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.area')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <edit-fence v-model="model.area" :center.sync="model.center" :shapeType="model.shapeType"></edit-fence>
              <!-- <a-textarea v-model="model.area" rows="4" placeholder="请输入围栏区域" /> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.remark')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" :placeholder="$t('common.form.rule.input',{label: $t('columns.remark')})" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('columns.status')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-select v-model="model.status" :placeholder="$t('common.form.rule.select',{label: $t('columns.status')})">
                <a-select-option :value="0">{{ $t('dict.status.0') }}</a-select-option>
                <a-select-option :value="1">{{ $t('dict.status.1') }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<i18n>
  {
    "zh-CN": {
      "columns": {
        "name": "围栏名称",
        "shapeType": "围栏形状",
        "area": "围栏区域",
        "remark": "围栏备注",
        "status": "状态"
      },
      "dict":{
        "status":{
          "0":"封存",
          "1":"启动"
        },
        "type": {
          "circle": "圆形",
          "polygon": "多边形"
        }
      }
    },
    "en-US": {
      "columns": {
        "name": "Fence Name",
        "area": "Fence Area",
        "shapeType": "Fence Shape",
        "remark": "Fence Remarks",
        "status": "State"
      },
      "dict":{
        "status":{
          "0":"Disenable",
          "1":"Enable"
        },
        "type": {
          "circle": "circle",
          "polygon": "polygon"
        }
      }
    },
    "fr-FR": {
      "columns": {
        "name": "Nom de la clôture",
        "area": "Zone de clôture",
        "shapeType": "Forme de clôture",
        "remark": "Remarques sur la clôture",
        "status": "Statut"
      },
      "dict":{
        "status":{
          "0":"Désactivé",
          "1":"Activer"
        },
        "type": {
          "circle": "Le cercle",
          "polygon": "Polygones"
        }
      }
    }
  }
</i18n>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import Edit from '@/components/map/fence/Edit'

  export default {
    name: 'RentalFenceForm',
    components: {
      EditFence: Edit
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/rental/fence/add",
          edit: "/rental/fence/edit",
          queryById: "/rental/fence/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>